package com.lagou.client;

import com.alibaba.fastjson.JSONObject;
import com.lagou.service.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class UserClientHandler extends ChannelInboundHandlerAdapter {

    private ChannelHandlerContext context;
    private String result;
    private RpcRequest rpcRequest;


    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        context = ctx;
    }

    /**
     * 收到服务端数据，唤醒等待线程
     */
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) {
        
        result = msg.toString();
        JSONObject jsonObject = JSONObject.parseObject(result);
        String channelId = jsonObject.getString("channelId");
        long timeCosts = System.currentTimeMillis() - RpcConsumer.startTimeMap.get(jsonObject.getString("requestId"));
        for(MyChannel myChannel : RpcConsumer.myChannelList){
            if(myChannel.getId().equals(channelId)) {
                myChannel.setLastTimeCosts(timeCosts);
            }
        }
        
        System.out.println(result);
    }

    /**
     * 设置参数
     */
    void setPara(RpcRequest para) {
        this.rpcRequest = para;
    }
    
    



}
